package com.ruoyi.yyq.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.yyq.domain.CarMerchantVerification;
import com.ruoyi.yyq.service.ICarMerchantVerificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.yyq.domain.CarMerchant;
import com.ruoyi.yyq.service.ICarMerchantService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商户Controller
 * 
 * @author ruoyi
 * @date 2025-05-12
 */
@RestController
@RequestMapping("/merchant")
@CrossOrigin
public class CarMerchantController extends BaseController
{
    @Autowired
    private ICarMerchantService carMerchantService;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private ICarMerchantVerificationService carMerchantVerificationService;
    /**
     * 查询商户列表
     */
    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/list")
    public TableDataInfo list(CarMerchant carMerchant)
    {
        startPage();
        List<CarMerchant> list = carMerchantService.selectCarMerchantList(carMerchant);
        return getDataTable(list);
    }

    /**
     * 导出商户列表
     */
    @RequiresPermissions("merchant:merchant:export")
    @Log(title = "商户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarMerchant carMerchant)
    {
        List<CarMerchant> list = carMerchantService.selectCarMerchantList(carMerchant);
        ExcelUtil<CarMerchant> util = new ExcelUtil<CarMerchant>(CarMerchant.class);
        util.exportExcel(response, list, "商户数据");
    }

    /**
     * 获取商户详细信息
     */
    @RequiresPermissions("merchant:merchant:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(carMerchantService.selectCarMerchantById(id));
    }

    /**
     * 新增商户
     */
    @RequiresPermissions("merchant:merchant:add")
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarMerchant carMerchant)
    {
        return toAjax(carMerchantService.insertCarMerchant(carMerchant));
    }

    /**
     * 新增商户
     */
//    @RequiresPermissions("merchant:merchant:add")
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @PostMapping("register")
    public AjaxResult register(@RequestBody CarMerchant carMerchant)
    {
        int i = carMerchantService.insertCarMerchant(carMerchant);
        if(i>0){
            return AjaxResult.success(carMerchant.getId());
        }
        return toAjax(i);
    }
    /**
     * 修改商户
     */
    @RequiresPermissions("merchant:merchant:edit")
    @Log(title = "商户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarMerchant carMerchant)
    {
        Long mid = carMerchant.getId();
        Long status = carMerchant.getAuditStatusId();
        if(status==1L){
            CarMerchantVerification carMerchantVerification = carMerchantVerificationService.selectCarMerchantVerificationByMid(mid);
            if(carMerchantVerification==null){
                return AjaxResult.error("该店铺还没有商户认证信息,审核失败,请先进行商户认证");
            }
            SysUser sysUser1 = remoteUserService.getUserInfo2(carMerchantVerification.getMobile(), SecurityConstants.INNER);
            Long userId = sysUser1.getUserId();
            remoteUserService.insertAuthRole(userId, new Long[]{100L}, SecurityConstants.INNER);
        }
        return toAjax(carMerchantService.updateCarMerchant(carMerchant));
    }

    /**
     * 删除商户
     */
    @RequiresPermissions("merchant:merchant:remove")
    @Log(title = "商户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(carMerchantService.deleteCarMerchantByIds(ids));
    }

    /**
     * 根据商家名模糊查询
     */
    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/search")
    public TableDataInfo searchByName(@RequestParam String name) {
        startPage();
        List<CarMerchant> list = carMerchantService.searchCarMerchantByName(name);
        return getDataTable(list);
    }

    /**
     * 根据服务类别查询商户列表
     */
    @GetMapping("/listByServiceType/{typeId}")
    public TableDataInfo listByServiceType(@PathVariable("typeId") Long typeId) {
        startPage();
        List<CarMerchant> list = carMerchantService.selectCarMerchantByServiceType(typeId);
        return getDataTable(list);
    }
}
